package com.yesun.task.pipeline.handler.impl;

import com.yesun.core.util.FileUtils;
import com.yesun.core.util.HtmlUtil;
import com.yesun.server.mdp.service.client.config.HttpConfig;
import com.yesun.server.mdp.service.client.impl.DefaultClient;
import com.yesun.server.mdp.service.client.impl.http.HttpRequestContext;
import com.yesun.server.mdp.service.client.impl.http.HttpResponseContext;
import com.yesun.server.mdp.service.client.utils.SpiderUtil;
import com.yesun.task.pipeline.common.Constant;
import com.yesun.task.pipeline.common.StockCode;
import com.yesun.task.pipeline.domain.dto.JobDto;
import com.yesun.task.pipeline.domain.dto.TaskDto;
import com.yesun.task.pipeline.handler.Executor;
import com.yesun.task.pipeline.handler.Handler;
import com.yesun.task.pipeline.handler.Loader;
import com.yesun.task.pipeline.worker.ParallelWorker;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.Element;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.BlockingQueue;

//jka龙头股
@Slf4j
public class LongtouHandler extends Handler {

    static DefaultClient client = new DefaultClient();
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyyMMdd");
    static String TODAY = shortSdf.format(Calendar.getInstance().getTime());

    public LongtouHandler(JobDto jobDto) {
        super(jobDto);
    }

    @Override
    public Loader getLoader() {
        //实例化
        return new Loader() {
            @Override
            public void load(BlockingQueue queue) {
                log.info("加载龙头股数据采集子任务...");
                //实现资金流入子任务，插入到队列
                String[] arr = StockCode.STOCK_CODE.split(",");
                for(String code : arr) {
                    //http://basic.10jqka.com.cn/002902/concept.html
                    String url = "http://basic.10jqka.com.cn/"+code +"/concept.html";
                    TaskDto taskDto = new TaskDto();
                    taskDto.setJobName(jobDto.getJobName());
                    taskDto.setName(code + "龙头股数据采集任务");
                    taskDto.setTag(Constant.JOB_TAG_LONGTOU);

                    Map<String,String> map = new HashMap<>();
                    map.put("url", url);
                    map.put("code", code);
                    map.put("saveTo", "/df_longtou/"+ shortSdf.format(new Date()) +"/"+code+".csv");//暂时只支持保存到磁盘
                    taskDto.setMap(map);


                    //加入MAP_TASK
                    ParallelWorker.MAP_TASK.put(taskDto.getName(), taskDto.getJobName());

                    queue.offer(taskDto);
                    log.info("添加子任务：" + taskDto.getName());
                }
            }
        };
    }

    @Override
    public Executor getExecutor() {
        return new Executor() {
            @Override
            public void execute(TaskDto taskDto) {
                //执行task的采集
                log.info("execute：" + taskDto.getName());

                if(new File(Constant.BASE_PATH + taskDto.getMap().get("saveTo")).exists()) {
                    log.info("文件存在，跳过！！");
                    return;
                }

                HttpConfig config = new HttpConfig(taskDto.getMap().get("url"), HttpConfig.HTTP_METHOD_GET);
                config.setEnctype("");
                config.setEncoding("GBK");
                HttpRequestContext request = new HttpRequestContext(config);
                try {
                    HttpResponseContext response = (HttpResponseContext) client.execute(request);
                    Document document = response.getContentAsDocument();
//                    log.info(document.asXML());
                    StringBuffer sbResult = new StringBuffer("采集日期,股票代码,概念,龙头股,概念描述\n");
                    Element element = (Element) document.selectNodes("//table[@class='gnContent']").get(0);
                    if(element != null) {
                        List<Element> list = element.selectNodes("tbody/tr");
                        int i=0;
                        for(Element ele : list) {
                            i++;
                            if(i % 2 == 0) {
                                continue;
                            }
                            Element elemTheme = (Element) ele.selectNodes("td").get(1);
                            Element elemLongtou = (Element) ele.selectNodes("td").get(2);
                            Element elemDescription = (Element) ele.selectNodes("td").get(3);
                            String theme = elemTheme.getText().trim();
                            sbResult.append(TODAY+ "," + taskDto.getMap().get("code") + ",");
                            //概念
                            sbResult.append(elemTheme.getText().trim() + ",");
                            //遍历龙头
                            List<Element> listLongtou = elemLongtou.selectNodes("a");
                            for(Element elemA : listLongtou) {
                                sbResult.append(elemA.attributeValue("code")+"|");//+"-"+elemA.getText().trim() + "|");
                            }
                            sbResult.append(",");
                            String description = HtmlUtil.cleanHtmlTag(list.get(i).asXML());
                            description = description.replaceAll("\\s*", "");
                            description = description.replaceAll(",", "，");
                            description = description.replace("    ", "");
                            sbResult.append(description + "\r\n");
                        }
                    }
//                    log.info(sbResult.toString());
                    //save to
                    FileUtils.saveCsv(Constant.BASE_PATH + taskDto.getMap().get("saveTo"), sbResult.toString(), "UTF-8");
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }
}
